package cn.com.nes.common.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.commons.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class PdfToImageUtil {
	protected static final Logger log = LoggerFactory.getLogger(PPTUtil.class);
	
	public static final float DEFAULT_DPI = 120;
	  //默认转换的图片格式为jpg
	  public static final String DEFAULT_FORMAT = "png";

    /**
     * PDF转图片
     *
     * @param fileContent PDF文件的二进制流
     * @return 图片文件的二进制流
     */
    public static List<byte[]> pdfToImage(byte[] fileContent) throws IOException {
        List<byte[]> result = new ArrayList<>();
        try (PDDocument document = PDDocument.load(fileContent)) {
            PDFRenderer renderer = new PDFRenderer(document);
            for (int i = 0; i < document.getNumberOfPages(); ++i) {
                BufferedImage bufferedImage = renderer.renderImageWithDPI(i, DEFAULT_DPI);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                ImageIO.write(bufferedImage, DEFAULT_FORMAT, out);
                result.add(out.toByteArray());
            }
        }
        return result;
    }
    
    public static List<byte[]> pdfToImage(String pdfPath) {
    	List<byte[]> imgList  = null ; 
        try {
          //图像合并使用参数
          // 总宽度
          int width = 0;
          // 保存一张图片中的RGB数据
          int[] singleImgRGB;
          int shiftHeight = 0;
          //保存每张图片的像素值
          BufferedImage imageResult = null;
          //利用PdfBox生成图像
          PDDocument pdDocument = PDDocument.load(new File(pdfPath));
          PDFRenderer renderer = new PDFRenderer(pdDocument);
          
          imgList  = new ArrayList<>(pdDocument.getNumberOfPages());
          
          //循环每个页码
          for (int i = 0, len = pdDocument.getNumberOfPages(); i < len; i++) {
        	  BufferedImage image = renderer.renderImageWithDPI(i, DEFAULT_DPI, ImageType.RGB);
              int imageHeight = image.getHeight();
              int imageWidth = image.getWidth();
              //计算高度和偏移量
              //使用第一张图片宽度;
              width = imageWidth;
              //保存每页图片的像素值
              imageResult = new BufferedImage(width, imageHeight, BufferedImage.TYPE_INT_RGB);
              //这里有高度，可以将imageHeight*len，我这里值提取一页所以不需要
               singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);
               // 写入流中
               imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width);
               
               ByteArrayOutputStream out = new ByteArrayOutputStream();
               javax.imageio.ImageIO.write(imageResult, DEFAULT_FORMAT, out);
               imgList.add(out.toByteArray());
               IOUtils.closeQuietly(out);
          }
          pdDocument.close();
          // 写图片
          
        } catch (Exception e) {
          e.printStackTrace();
        }
        //OVER
        return   imgList  ; 
      } 
    
    public static void main(String[] args) throws IOException {
        // 读入PPT文件
        String filePath = "D:/ppt/";
        String fileName = "718智慧养殖.pdf";
        
        List<byte[]> images = pdfToImage(filePath+fileName);
        // 输出图片
        for (int i = 1; i <= images.size(); i++) {
            String path = filePath + i + ".png";
            FileOutputStream output = new FileOutputStream(path);
            IOUtils.write(images.get(i-1), output);
            IOUtils.closeQuietly(output);
        }
 
        System.out.println("OK");
	}
    
    
}
